import { Route } from 'react-router-dom';

import { baseRouters } from '@/router';
import { flatRoutes } from '@/utils/routeUtils';
import { NOTFOUND, ACCOUNT, ASK, SEARCH, MY_QA_DETAIL, ANSWER_GOLD, PICK_UP, SETTING, FAVORITES, BROWSING, MESSAGE} from '@/config/url';

const NEED_LOGIN_URL = [ACCOUNT, ASK, SEARCH, MY_QA_DETAIL, ANSWER_GOLD, PICK_UP, SETTING, FAVORITES, BROWSING, MESSAGE];
/**
 * 路由守卫
 * 1. 菜单是后端返回的，所以不会存在403页面的问题
 * 2. 是否登录这个判断逻辑是否需要配置在路由上？TODO - 目前是所有页面没登录都重定向到首页
*/
const PrivateRoute = (props) => {
    let baseRoutes = flatRoutes(baseRouters), // 用来找NotFound页面用的，appRoutes可能为空
        appRoutes = window.SITE_CONFIG?.appRouteArray || [],
        {component: Comp, ...rest} = props,
        NotFound = baseRoutes.find(i => i.url === NOTFOUND)?.component || null,
        existPage = appRoutes.find(i => i.url === location.pathname);

    return (
        <Route
            {...rest}
            render={(props) => {
                if(!existPage) { // 页面不存在
                    return <NotFound {...props}/>
                }

                return <Comp {...props} />;
            }}
        />
    );
}

export default PrivateRoute;
